/* 
 * Copyright (c) 2006-2014, Ivan Jeukens
 * All rights reserved.
 *
 * This software is open-source under GNU General Public License, version 2 
 * see LICENSE file
 */
package odog.editor;

import odog.codegen.CodeGeneratorControl;
import odog.configuration.BaseConfiguration;
import odog.design.ComponentArtifact;
import odog.design.Artifact;
import odog.design.Artifact.ElementType;
import odog.design.Design;
import odog.design.DesignConfiguration;
import odog.design.DesignRepository;
import odog.editor.ArtifactEditors.ArtifactEditor;
import odog.editor.configuration.configurationPanel;
import odog.editor.ArtifactEditors.ArtifactEditor.EditorType;
import odog.editor.ArtifactEditors.ComponentArtifactEditor;
import odog.editor.ArtifactEditors.AtomicComponentEditor;
import odog.editor.ArtifactEditors.CompositeComponentEditor;
import odog.editor.ArtifactEditors.RuleEditor;
import odog.editor.configuration.RuleConfiguration;
import odog.editor.configuration.RuleDescription;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URL;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import odog.design.AtomicComponent;
import odog.design.CompositeComponent;
import odog.design.RuleCheckingStatus;
import odog.design.RuleElement;
import odog.ruleChecker.Checker;
import odog.ruleChecker.Rule;
import odog.syntax.Nodes.Acomp;
import odog.syntax.Nodes.Attr;
import odog.syntax.Nodes.Hver;
import odog.syntax.Nodes.Topology;
import odog.syntax.Parse.AtomicComponentParser;
import odog.syntax.Parse.TopologyParser;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import odog.design.DesignParser;

/**
 *
 * @author  ivan
 */
public class OdogEditor extends javax.swing.JFrame {
    
    /** Creates new form OdogEditor */
    public OdogEditor() {
        initComponents();

        configuration = new BaseConfiguration();
        designRepository = new DesignRepository(configuration);

        projectTreeState = new TreeState("projects", projectTree, configuration,
                designRepository);
        projectTreeState.loadState();

        environmentLibs = loadEnvironmentLibs();
        libraryViewerFrame = new LibraryViewerFrame(configuration, designRepository, 
                environmentLibs);
        libraryViewerFrame.loadState();

        aboutFrame = new AboutFrame();
        
//        servicesFrame = new ServicesFrame(configuration);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        projectTreeMenu = new javax.swing.JPopupMenu();
        newProjectMenuItem = new javax.swing.JMenuItem();
        importProjectMenuItem = new javax.swing.JMenuItem();
        projectMenu = new javax.swing.JPopupMenu();
        jMenuItem5 = new javax.swing.JMenuItem();
        removeProject = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        addArtifact = new javax.swing.JMenuItem();
        importArtifact = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        checkRules = new javax.swing.JMenuItem();
        generateCode = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JSeparator();
        configure = new javax.swing.JMenuItem();
        projectArtifactMenu = new javax.swing.JPopupMenu();
        saveArtifact = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JSeparator();
        editArtifact = new javax.swing.JMenuItem();
        viewArtifact = new javax.swing.JMenuItem();
        removeArtifact = new javax.swing.JMenuItem();
        jSeparator5 = new javax.swing.JSeparator();
        checkArtifact = new javax.swing.JMenuItem();
        viewCheckResults = new javax.swing.JMenuItem();
        newProjectDialog = new javax.swing.JDialog();
        jLabel1 = new javax.swing.JLabel();
        newProjectDialog_jTextField1 = new javax.swing.JTextField();
        newProjectDialog_jTextField2 = new javax.swing.JTextField();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        newProjectDialog_SystemButton = new javax.swing.JRadioButton();
        newProjectDialog_LibraryButton = new javax.swing.JRadioButton();
        locationChooser = new javax.swing.JFileChooser();
        addArtifactDialog = new javax.swing.JDialog();
        jLabel3 = new javax.swing.JLabel();
        addArtifactDialog_nameTextField = new javax.swing.JTextField();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        addArtifactDialog_artifactList = new javax.swing.JList();
        newProjectButtonGroup = new javax.swing.ButtonGroup();
        checkResultsFrame = new javax.swing.JFrame();
        jScrollPane2 = new javax.swing.JScrollPane();
        checkResultsTextArea = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        jSplitPane2 = new javax.swing.JSplitPane();
        jPanel2 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        projectTree = new javax.swing.JTree();
        pane3 = new javax.swing.JPanel();
        editorPane = new javax.swing.JLayeredPane();
        mainMenu = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jSeparator6 = new javax.swing.JSeparator();
        jMenuItem4 = new javax.swing.JMenuItem();

        newProjectMenuItem.setComponentPopupMenu(projectTreeMenu);
        newProjectMenuItem.setText("New Project");
        newProjectMenuItem.setToolTipText("Creates a New Project");
        newProjectMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newProjectMenuItemActionPerformed(evt);
            }
        });
        projectTreeMenu.add(newProjectMenuItem);

        importProjectMenuItem.setText("Import Project");
        importProjectMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                importProjectMenuItemActionPerformed(evt);
            }
        });
        projectTreeMenu.add(importProjectMenuItem);

        jMenuItem5.setText("Save project");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        projectMenu.add(jMenuItem5);

        removeProject.setText("Remove Project");
        removeProject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeProjectActionPerformed(evt);
            }
        });
        projectMenu.add(removeProject);
        projectMenu.add(jSeparator2);

        addArtifact.setActionCommand("Add artifact");
        addArtifact.setLabel("Add artifact");
        addArtifact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addArtifactActionPerformed(evt);
            }
        });
        projectMenu.add(addArtifact);

        importArtifact.setText("Import artifact");
        importArtifact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                importArtifactActionPerformed(evt);
            }
        });
        projectMenu.add(importArtifact);
        projectMenu.add(jSeparator1);

        checkRules.setText("Check rules");
        checkRules.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkRulesActionPerformed(evt);
            }
        });
        projectMenu.add(checkRules);

        generateCode.setText("Generate code");
        generateCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateCodeActionPerformed(evt);
            }
        });
        projectMenu.add(generateCode);
        projectMenu.add(jSeparator3);

        configure.setText("Configure");
        configure.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                configureActionPerformed(evt);
            }
        });
        projectMenu.add(configure);

        saveArtifact.setText("Save Artifact");
        saveArtifact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveArtifactActionPerformed(evt);
            }
        });
        projectArtifactMenu.add(saveArtifact);
        projectArtifactMenu.add(jSeparator4);

        editArtifact.setText("Edit");
        editArtifact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editArtifactActionPerformed(evt);
            }
        });
        projectArtifactMenu.add(editArtifact);

        viewArtifact.setText("View");
        viewArtifact.setEnabled(false);
        viewArtifact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewArtifactActionPerformed(evt);
            }
        });
        projectArtifactMenu.add(viewArtifact);

        removeArtifact.setText("Remove");
        removeArtifact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeArtifactActionPerformed(evt);
            }
        });
        projectArtifactMenu.add(removeArtifact);
        projectArtifactMenu.add(jSeparator5);

        checkArtifact.setText("Check");
        checkArtifact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkArtifactActionPerformed(evt);
            }
        });
        projectArtifactMenu.add(checkArtifact);

        viewCheckResults.setText("View Check results");
        viewCheckResults.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewCheckResultsActionPerformed(evt);
            }
        });
        projectArtifactMenu.add(viewCheckResults);

        newProjectDialog.setTitle("New project");
        newProjectDialog.setBounds(new java.awt.Rectangle(400, 400, 600, 180));
        newProjectDialog.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        newProjectDialog.setModal(true);
        newProjectDialog.setResizable(false);

        jLabel1.setText("Project name :");

        newProjectDialog_jTextField1.setFont(new java.awt.Font("Dialog", 0, 14));

        newProjectDialog_jTextField2.setFont(new java.awt.Font("Dialog", 0, 14));
        newProjectDialog_jTextField2.setToolTipText("Specify the directory that the project's files will be saved");

        jButton5.setText("Location");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("Done");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jLabel2.setText("Design Type:");

        newProjectButtonGroup.add(newProjectDialog_SystemButton);
        newProjectDialog_SystemButton.setSelected(true);
        newProjectDialog_SystemButton.setText("System");
        newProjectDialog_SystemButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        newProjectDialog_SystemButton.setMargin(new java.awt.Insets(0, 0, 0, 0));

        newProjectButtonGroup.add(newProjectDialog_LibraryButton);
        newProjectDialog_LibraryButton.setText("Library");
        newProjectDialog_LibraryButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        newProjectDialog_LibraryButton.setMargin(new java.awt.Insets(0, 0, 0, 0));

        javax.swing.GroupLayout newProjectDialogLayout = new javax.swing.GroupLayout(newProjectDialog.getContentPane());
        newProjectDialog.getContentPane().setLayout(newProjectDialogLayout);
        newProjectDialogLayout.setHorizontalGroup(
            newProjectDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(newProjectDialogLayout.createSequentialGroup()
                .addGroup(newProjectDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(newProjectDialogLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(newProjectDialogLayout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(newProjectDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jButton5))))
                .addGroup(newProjectDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(newProjectDialogLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(newProjectDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(newProjectDialog_jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 540, Short.MAX_VALUE)
                            .addComponent(newProjectDialog_jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 540, Short.MAX_VALUE)))
                    .addGroup(newProjectDialogLayout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(newProjectDialog_SystemButton)
                        .addGap(21, 21, 21)
                        .addComponent(newProjectDialog_LibraryButton)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, newProjectDialogLayout.createSequentialGroup()
                .addContainerGap(284, Short.MAX_VALUE)
                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(229, 229, 229))
        );
        newProjectDialogLayout.setVerticalGroup(
            newProjectDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(newProjectDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(newProjectDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(newProjectDialog_jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(newProjectDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(newProjectDialog_jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton5))
                .addGap(22, 22, 22)
                .addGroup(newProjectDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(newProjectDialog_SystemButton)
                    .addComponent(newProjectDialog_LibraryButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addComponent(jButton6)
                .addContainerGap())
        );

        locationChooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);

        addArtifactDialog.setTitle("Add artifact");
        addArtifactDialog.setBounds(new java.awt.Rectangle(200, 100, 500, 330));
        addArtifactDialog.setModal(true);
        addArtifactDialog.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                addArtifactDialogWindowClosing(evt);
            }
        });

        jLabel3.setText("Name:");

        jButton7.setText("Ok");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jButton8.setText("Cancel");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jLabel4.setText("Artifact Editor:");

        addArtifactDialog_artifactList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Atomic Component", "Composite Component", "Rule" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        addArtifactDialog_artifactList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane4.setViewportView(addArtifactDialog_artifactList);

        javax.swing.GroupLayout addArtifactDialogLayout = new javax.swing.GroupLayout(addArtifactDialog.getContentPane());
        addArtifactDialog.getContentPane().setLayout(addArtifactDialogLayout);
        addArtifactDialogLayout.setHorizontalGroup(
            addArtifactDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, addArtifactDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(addArtifactDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(addArtifactDialogLayout.createSequentialGroup()
                        .addGap(185, 185, 185)
                        .addComponent(jButton7)
                        .addGap(22, 22, 22)
                        .addComponent(jButton8))
                    .addGroup(addArtifactDialogLayout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addArtifactDialog_nameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 463, Short.MAX_VALUE))
                    .addGroup(addArtifactDialogLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(20, 20, 20)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE)))
                .addContainerGap())
        );
        addArtifactDialogLayout.setVerticalGroup(
            addArtifactDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addArtifactDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(addArtifactDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(addArtifactDialog_nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(addArtifactDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(addArtifactDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton7)
                    .addComponent(jButton8))
                .addGap(82, 82, 82))
        );

        checkResultsTextArea.setColumns(20);
        checkResultsTextArea.setEditable(false);
        checkResultsTextArea.setRows(5);
        jScrollPane2.setViewportView(checkResultsTextArea);

        checkResultsFrame.getContentPane().add(jScrollPane2, java.awt.BorderLayout.CENTER);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("ODOG Editor");
        setBounds(new java.awt.Rectangle(100, 30, 800, 600));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jSplitPane2.setDividerLocation(400);

        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel2.setComponentPopupMenu(projectTreeMenu);

        projectTree.setDragEnabled(true);
        projectTree.setFocusable(false);
        projectTree.setModel(null);
        projectTree.setShowsRootHandles(true);
        projectTree.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                projectTreeMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(projectTree);

        jTabbedPane1.addTab("Projecs", jScrollPane1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 678, Short.MAX_VALUE)
        );

        jTabbedPane1.getAccessibleContext().setAccessibleName("Projects");

        jSplitPane2.setLeftComponent(jPanel2);

        javax.swing.GroupLayout pane3Layout = new javax.swing.GroupLayout(pane3);
        pane3.setLayout(pane3Layout);
        pane3Layout.setHorizontalGroup(
            pane3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(editorPane, javax.swing.GroupLayout.DEFAULT_SIZE, 653, Short.MAX_VALUE)
        );
        pane3Layout.setVerticalGroup(
            pane3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(editorPane, javax.swing.GroupLayout.DEFAULT_SIZE, 682, Short.MAX_VALUE)
        );

        jSplitPane2.setRightComponent(pane3);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 1064, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 684, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jMenu1.setText("Configurations");

        jMenuItem1.setText("Library Index");
        jMenuItem1.setActionCommand("New Project");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem3.setText("View Services");
        jMenuItem3.setEnabled(false);
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        mainMenu.add(jMenu1);

        jMenu2.setText("Help");

        jMenuItem2.setText("List predefined attributes");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuItem6.setText("List communication methods");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem6);

        jMenuItem7.setText("List services");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem7);
        jMenu2.add(jSeparator6);

        jMenuItem4.setText("About");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem4);

        mainMenu.add(jMenu2);

        setJMenuBar(mainMenu);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
        JFrame frame = new JFrame("Services");
        String loc = "file:" + configuration.getOdogWorkspace() + "/helpFiles/services.html";
        try {
            URL url = new URL(loc);
            HtmlViewer pane = new HtmlViewer(url);

            frame.setContentPane(pane);
            frame.setSize(800,700);        
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setVisible(true);
        }
        catch(Exception ex) {
            System.out.println(ex);
        }
    }//GEN-LAST:event_jMenuItem7ActionPerformed

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        JFrame frame = new JFrame("Communication methods");
        String loc = "file:" + configuration.getOdogWorkspace() + "/helpFiles/communicationMethods.html";
        try {
            URL url = new URL(loc);
            HtmlViewer pane = new HtmlViewer(url);

            frame.setContentPane(pane);
            frame.setSize(800,700);        
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setVisible(true);
        }
        catch(Exception ex) {
            System.out.println(ex);
        }
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        JFrame frame = new JFrame("Predefined attributes");
        String loc = "file:" + configuration.getOdogWorkspace() + "/helpFiles/reservedAttributes.html";
        try {
            URL url = new URL(loc);
            HtmlViewer pane = new HtmlViewer(url);

            frame.setContentPane(pane);
            frame.setSize(800,700);        
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setVisible(true);
        }
        catch(Exception ex) {
            System.out.println(ex);
        }
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        // show about frame
        aboutFrame.setVisible(true);
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void generateCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateCodeActionPerformed
        executeProjectAction(ActionType.GENERATE_CODE);
    }//GEN-LAST:event_generateCodeActionPerformed

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        executeProjectAction(ActionType.SAVE_DESIGN);
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void checkRulesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkRulesActionPerformed
        executeProjectAction(ActionType.CHECK_RULES);
    }//GEN-LAST:event_checkRulesActionPerformed

    private void viewArtifactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewArtifactActionPerformed
        //executeProjectAction(ActionType.VIEW_ARTIFACT);
    }//GEN-LAST:event_viewArtifactActionPerformed

    private void viewCheckResultsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewCheckResultsActionPerformed
        executeProjectAction(ActionType.VIEW_CHECK_RESULTS);
    }//GEN-LAST:event_viewCheckResultsActionPerformed

    private void checkArtifactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkArtifactActionPerformed
        executeProjectAction(ActionType.CHECK_ARTIFACT);
    }//GEN-LAST:event_checkArtifactActionPerformed

    private void projectTreeMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_projectTreeMouseReleased
        if(!(evt.getButton() != 1)) return;

        int xCoord = evt.getX();
        int yCoord = evt.getY();

        TreePath path = projectTree.getPathForLocation(xCoord, yCoord);
        if(path != null) {
            Object last = path.getLastPathComponent();
            if(last instanceof DefaultMutableTreeNode) {
                projectTree.setSelectionPath(path);
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) last;
                
                Object obj = node.getUserObject();
                if(obj instanceof String) {
                    projectTreeMenu.show(projectTree, xCoord + 15, yCoord);
                } 
                else
                if(obj instanceof Design) {
                    projectMenu.show(projectTree, xCoord + 15, yCoord);
                } 
                else
                if(obj instanceof Artifact) {
                    projectArtifactMenu.show(projectTree, xCoord + 15, yCoord);
                }
            }
        }
    }//GEN-LAST:event_projectTreeMouseReleased

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        // servicesFrame.setVisible(true);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        libraryViewerFrame.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void saveArtifactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveArtifactActionPerformed
        executeProjectAction(ActionType.SAVE_ARTIFACT);
    }//GEN-LAST:event_saveArtifactActionPerformed

    private void editArtifactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editArtifactActionPerformed
        executeProjectAction(ActionType.EDIT_ARTIFACT);
    }//GEN-LAST:event_editArtifactActionPerformed

    private void configureActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_configureActionPerformed
        executeProjectAction(ActionType.CONFIGURE_DESIGN);
    }//GEN-LAST:event_configureActionPerformed

    private void removeArtifactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeArtifactActionPerformed
        executeProjectAction(ActionType.REMOVE_ARTIFACT);
    }//GEN-LAST:event_removeArtifactActionPerformed

    private void importArtifactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_importArtifactActionPerformed
        executeProjectAction(ActionType.IMPORT_ARTIFACT);
    }//GEN-LAST:event_importArtifactActionPerformed

    private void importProjectMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_importProjectMenuItemActionPerformed
        executeProjectAction(ActionType.IMPORT_DESIGN);
    }//GEN-LAST:event_importProjectMenuItemActionPerformed

    private void addArtifactDialogWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_addArtifactDialogWindowClosing
        addArtifactOk = false;
    }//GEN-LAST:event_addArtifactDialogWindowClosing

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        addArtifactDialog.setVisible(false);
        addArtifactOk = true;
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        addArtifactDialog.setVisible(false);
        addArtifactOk = false;
    }//GEN-LAST:event_jButton8ActionPerformed

    private void addArtifactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addArtifactActionPerformed
        executeProjectAction(ActionType.ADD_ARTIFACT);
    }//GEN-LAST:event_addArtifactActionPerformed

    private void removeProjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeProjectActionPerformed
        executeProjectAction(ActionType.REMOVE_DESIGN);
    }//GEN-LAST:event_removeProjectActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        locationChooser.setCurrentDirectory(new File(configuration.getOdogWorkspace()));
        int returnVal = locationChooser.showOpenDialog(newProjectDialog);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            newProjectDialog_jTextField2.setText(locationChooser.getSelectedFile().toString());   
        }
    }//GEN-LAST:event_jButton5ActionPerformed
    
    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        projectTreeState.saveState();
        if(libraryViewerFrame != null) {
            libraryViewerFrame.saveState();
        }
    }//GEN-LAST:event_formWindowClosing

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        newProjectDialog.setVisible(false);
    }//GEN-LAST:event_jButton6ActionPerformed

    private void newProjectMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newProjectMenuItemActionPerformed
        executeProjectAction(ActionType.NEW_DESIGN);
    }//GEN-LAST:event_newProjectMenuItemActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new OdogEditor().setVisible(true);
            }
        });
    }

    /* Este metodo dispara a acao respectiva para um dado modelo. Ele deve ser chamado
     * como resposta de eventos nas arvores de projeto e biblioteca */
    public void executeProjectAction(ActionType action) {
        DefaultMutableTreeNode selectedNode = projectTreeState.getCurrentSelectedNode();
        Object userObject = selectedNode.getUserObject();
        switch(action) {

            /////////////////////////////////////////////////// Sobre o toplevel

            case NEW_DESIGN: {
                newProjectDialog.setVisible(true);

                String projectName = newProjectDialog_jTextField1.getText();
                String location = newProjectDialog_jTextField2.getText();

                if(projectName == null || location == null || projectName.matches("[\\s]*")
                        || location.matches("[\\s]*")) {       
                    
                    JOptionPane.showMessageDialog(this, "Missing project name and location.");
                    return;
                }

                location = BaseConfiguration.appendSlash(location);
                boolean islib;
                if(newProjectDialog_SystemButton.isSelected()) {
                    islib = false;
                }
                else {
                    islib = true;
                }
                clearNewProjectDialog();

                if(projectName != null && location != null && !projectName.equals("") &&
                        !location.equals("")) {
                    if(!designRepository.contains(location + projectName + ".xml")) {                       
                        Design p = new Design(projectName, location, islib);
                        projectTreeState.addNewDesign(p);
                        designRepository.addDesign(p, location + projectName + ".xml");
                        
                        File f = new File(location);
                        if(!f.exists()) {
                            f.mkdir();
                        }

                        p.saveDesign();
                        
                        libraryViewerFrame.addDesignDirectory(location + projectName + 
                                ".xml");
                    }
                    else {
                        JOptionPane.showMessageDialog(this, "There is already a project at:\n" +
                                location + "\n");
                    }
                    projectTreeState.reloadTree();
                }
            } break;                        

            case IMPORT_DESIGN: {
                JFileChooser dir = new JFileChooser(configuration.getOdogWorkspace());
                dir.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);                
                int returnVal = dir.showOpenDialog(this);
                if(returnVal == JFileChooser.APPROVE_OPTION) {
                    File designFile = dir.getSelectedFile();
                    if(projectTreeState.containsDesign(designFile.getAbsolutePath())) {
                        JOptionPane.showMessageDialog(this, "Already working on " +
                                "design:\n " + designFile);
                        return;
                    }

                    try {
                        // this line is necessary for parsing the design. The name 
                        // should not have the .xml. It will be reset inside DesignParser
                        Design d = designRepository.loadDesignFromFile(designFile.getAbsolutePath(),
                                false);
                        if(d == null) {
                           JOptionPane.showMessageDialog(this, "File " + designFile +
                                    "\nis not a valid design file.");
                           System.out.println(DesignParser.errorMessage);
                           return;
                        }

                        projectTreeState.addNewDesign(d);                        
                        projectTreeState.reloadTree();
                        
                        libraryViewerFrame.addDesignDirectory(designFile.getAbsolutePath());
                    }
                    catch(Exception ex) {
                        System.out.println(ex.toString());
                    }
                }
            } break;
            
            case SAVE_DESIGN: {
                Design p = (Design) userObject;
                p.saveDesign();
            } break;

            case REMOVE_DESIGN: {
                Design p = (Design) userObject;
                
                // 1. Pergunta se tem certeza
                int answer = JOptionPane.showConfirmDialog(this, "Are you sure about " +
                        "removing project:\n" + p.getDesignName());
                if(answer == JOptionPane.NO_OPTION ||
                    answer == JOptionPane.CANCEL_OPTION) {
                    return;
                } 
                
                // 2. Fechar todas as visualizacoes deste projeto                
                p.closeAllArtifactEditors();

                // 3. Perguntar se deve-se destruir completamente o diretorio onde
                // ele se encontra
/*
                answer = JOptionPane.showConfirmDialog(this, "Remove files under " +
                        p.getDesignLocation());
                if(answer == JOptionPane.YES_OPTION) {
                    File fd = new File(p.getDesignLocation());
                    deleteDir(fd);
                }  */

                // 4. Remover a entrada da arvore de projetos
                libraryViewerFrame.removeDesignDirectory(p.getDesignLocation() +
                        p.getDesignName() + ".xml"); 
                
                projectTreeState.removeCurrentSelectedDesign();
            } break;
            
            /////////////////////////////////////////////////// Sobre um design
            
            case ADD_ARTIFACT: {
                Design project = (Design) userObject;
                
                // 1. Mostra o dialog de criacao de artefato
                addArtifactDialog.setVisible(true);
                if(!addArtifactOk) {
                    clearAddArtifactDialog();
                    return;
                }
                
                // 2. Recupera o nome do artefato, e o ArtifactEditor correspondente
                String artiname = addArtifactDialog_nameTextField.getText();
                if(artiname == null || artiname.equals("")) {
                    clearAddArtifactDialog();
                    return;
                }
                else
                if(project.containsArtifact(artiname)) {
                    JOptionPane.showMessageDialog(this,"Project:\n" + project.getDesignName() +  
                            "already contains artifact:\n" + artiname);
                    return;
                }
                else
                if(project.getDesignName().equals(artiname)) {
                    JOptionPane.showMessageDialog(this,"Cannot name the artifact " +
                        " with the same name of the project");
                    return;
                }
                
                String editor = (String) addArtifactDialog_artifactList.getSelectedValue();
                EditorType edtype;

                clearAddArtifactDialog();

                // 3. Cria o LibraryElement associado ao artefado, e colocada
                // dentro do projeto. Informa o editor correto para ele.             
                Artifact element = null;
                if(editor.equals("Atomic Component")) {
                    element = AtomicComponent.newEmptyComponent(artiname, 
                            project.getDesignLocation());
                }
                else
                if(editor.equals("Composite Component")) {
                    element = CompositeComponent.newEmptyComponent(artiname,
                            project.getDesignLocation(), designRepository);
                }
                else 
                if(editor.equals("Rule")) {
                    element = RuleElement.newEmptyRule(artiname, 
                            project.getDesignLocation());
                }
                else {
                    return;
                }
                project.addArtifact(element);

                // 4. Atualiza a visao da arvore de projetos.
                projectTreeState.reloadTree();
                editorPane.repaint();
            } break;
            
            case IMPORT_ARTIFACT: {
                Design project = (Design) userObject;
                // 1. verifica se o arquivo em questao eh um xml valido para um
                // Acomp, topology ou rule
                EditorType edtype = null;

                JFileChooser dir = new JFileChooser(project.getDesignLocation());
                dir.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);                
                int returnVal = dir.showOpenDialog(this);
                
                String fileDir = null;   // this var records the subdir with respect to the project location that
                                         // the design file is located
                String elementName = null;
                File artifactFile = null;
                if(returnVal == JFileChooser.APPROVE_OPTION) {
                    artifactFile = dir.getSelectedFile();

                    fileDir = Design.subtractPath(artifactFile.getAbsolutePath(), 
                            project.getDesignLocation());
                    if(fileDir == null) {
                        JOptionPane.showMessageDialog(this, "Imported artifact must " + 
                            "reside within project dir:\n" + project.getDesignLocation());
                                return;
                    }
                    // testa primero se eh ator atomico
                    Acomp comp = AtomicComponentParser.parseAtomicComponent(
                            artifactFile.getAbsolutePath());
                    if(AtomicComponentParser.errorMessage == null) {
                        // deu certo
                        edtype = EditorType.ATOMIC;
                        elementName = comp.getName();
                    }
                    else {
                        // teste seh eh ator composite
                        Topology top = TopologyParser.parseTopology(
                                artifactFile.getAbsolutePath(), designRepository);
                        if(TopologyParser.errorMessage == null) {
                            edtype = EditorType.COMPOSITE;
                            elementName = top.getName();
                        }
                        else {
                            boolean b = RuleElement.testRuleFile(
                                    artifactFile.getAbsolutePath());
                            if(b) {
                                edtype = EditorType.RULE;
                                elementName = JOptionPane.showInputDialog("Enter rule name");
                            }
                            else {
                                JOptionPane.showMessageDialog(this, "File:\n" + 
                                    artifactFile.getAbsolutePath() +  "does not contain " +
                                        "a valid artifact file.");
                                return;
                            }
                        }
                    }
                }
                else {
                    return;
                }
                
                if(project.containsArtifact(elementName)) {
                    JOptionPane.showMessageDialog(this, "Project:\n" + project.getDesignName() +  
                            "already contains artifact:\n" + elementName);
                    return;
                }

                // 2. Cria um novo artefato e elemento de biblioteca correspondete
                Artifact element = null;
                URL url = null;
                if(edtype != EditorType.RULE) {
                    try {
                        url = new URL("file:" + fileDir);
                    }
                    catch(Exception ex) {
                        System.out.println(ex);
                        return;
                    }
                }
                else {
                    try {
                        url = new URL("file:" + fileDir);
                    }
                    catch(Exception ex) {
                        System.out.println(ex);
                        return;
                    }
                } 

                switch(edtype) {
                    case ATOMIC: {
                        element = new AtomicComponent(elementName, url, 
                                new Date(), project.getDesignLocation());
                    } break;
                    
                    case COMPOSITE: {
                        element = new CompositeComponent(elementName, 
                                url, new Date(), project.getDesignLocation(), 
                                designRepository);
                    } break;
                    
                    case RULE: {
                        element = new RuleElement(elementName, url,
                                new Date(), project.getDesignLocation());
                    } break;
                }
                project.addArtifact(element);
                // 4. Atualiza a visao da arvore de projetos.
                projectTreeState.reloadTree();                
            } break;

            case CHECK_RULES: {
                Design project = (Design) userObject;

                JDialog dialog = new JDialog();
                dialog.setModal(true);
                dialog.setSize(500,200);
                dialog.setLocation(this.getX() + 10, this.getY()+ 10);

                PickRuleConfigurationPanel pc = new PickRuleConfigurationPanel(dialog,
                        project.getConfiguration());
                dialog.setContentPane(new JScrollPane(pc));
                dialog.setVisible(true);
                
                if(pc.isOk()) {
                    RuleConfiguration rconf = pc.getSelectedRuleConfiguration();
                    for(Artifact art : project.artifacts()) {
                        if(art.getType() != ElementType.ATOMICCOMP &&
                                art.getType() != ElementType.COMPOSITECOMP) continue;
                        
                        ComponentArtifact aed = (ComponentArtifact) art;
                        RuleCheckingStatus stat = aed.getRuleCheckingStatus();
                        if(stat.wasChecked() && stat.hasPassed() && 
                                stat.getRuleConfiguration().equals(rconf.getName())) continue;

                        checkRules(rconf, aed);
                    }
                    project.getStatus().signalChanged();
                }
                
            } break;
            
            case GENERATE_CODE: {
                Design project = (Design) userObject;
                
                // 1. Find the toplevel component
                Topology toplevel = null;
                CompositeComponent toplevelComp = null;
                LinkedList<Topology> topcomps = new LinkedList<Topology>();
                LinkedList<CompositeComponent> toparts = new LinkedList<CompositeComponent>();
                for(Artifact art : project.artifacts()) {
                    if(art.getEditorType() == EditorType.COMPOSITE) {
                        toplevelComp = (CompositeComponent) art;
                        Topology top = toplevelComp.getRootNode();
                        
                        if(top == null) continue;
                        Attr at = top.getAttributeFromTable(top.getName() + ".Toplevel");
                        if(at != null) {
                            topcomps.add(top);
                            toparts.add(toplevelComp);
                        }
                    }
                }
                if(topcomps.size() == 0) {
                    JOptionPane.showMessageDialog(this, "In order to code generate\n" +
                            "at least one component must be tagged as toplevel");
                    return;
                }
                
                if(topcomps.size() == 1) {
                    toplevel = topcomps.get(0);
                    toplevelComp = toparts.get(0);
                }
                else {
                    Object[] possibleTops = topcomps.toArray();
                    toplevel = (Topology) JOptionPane.showInputDialog(null,
                        "Choose toplevel component", "Components", JOptionPane.INFORMATION_MESSAGE, 
                        null, possibleTops, possibleTops[0]);
                    if(toplevel == null) return;
                    
                    int index = topcomps.indexOf(toplevel);
                    toplevelComp = toparts.get(index);
                }
                
                // 2. Check if the toplevel has been checked for code generation rules and syntax
                RuleCheckingStatus cstatus = toplevelComp.getRuleCheckingStatus();
                if(!cstatus.wasChecked() ||
                   !cstatus.hasPassed()) {
                    JOptionPane.showMessageDialog(this, "Must run the rule checking before " +
                            " generating code.");
                    return;
                }
                else {
                    String s = cstatus.getRuleConfiguration();
                    RuleConfiguration rconf = project.getConfiguration().getRuleConfiguration(s);
                    if(!rconf.includeCGRules() || !rconf.includeSyntaxRules()) {
                        JOptionPane.showMessageDialog(this, "In order to code generate, " +
                            " code generation and syntax rules must be included\n" +
                            "in the design configuration.");   
                        return;
                    }
                }

                // 3. create the codegenerator object and list the possible platforms
                CodeGeneratorControl cgcontrol = new CodeGeneratorControl(configuration,
                        designRepository);
                Object[] possibleValues = cgcontrol.getAvailablePlatforms().toArray();
                if(possibleValues.length == 0) {
                    JOptionPane.showMessageDialog(this, "There must be at least one" +
                            " platform configured.");
                    return;
                }
                Object selectedPlatform;
                if(possibleValues.length == 1) {
                    selectedPlatform = possibleValues[0];
                }
                else {
                   selectedPlatform = JOptionPane.showInputDialog(null,
                        "Choose platform", "Platforms", JOptionPane.INFORMATION_MESSAGE, 
                        null,possibleValues, possibleValues[0]);
                   if(selectedPlatform == null) return;
                }
                                
                // 4. find out the versions of the topolevel topology
                Iterator vite = toplevel.versionsIterator();
                LinkedList<String> ll = new LinkedList<String>();
                while(vite.hasNext()) {
                    ll.add(((Hver)vite.next()).getName());
                }
                possibleValues = ll.toArray();
                Object selectedVersion;
                if(possibleValues.length == 1) {
                    selectedVersion = possibleValues[0];
                }
                else {
                   selectedVersion = JOptionPane.showInputDialog(null,
                      "Choose version", "Versions", JOptionPane.INFORMATION_MESSAGE, 
                        null,possibleValues, possibleValues[0]);
                   if(selectedVersion == null) return;
                }
                
                // 5. call the code generator object
                boolean res = cgcontrol.codeGenerate(project, (String)selectedPlatform, 
                        (String)selectedVersion, toplevel, toplevelComp);
                if(res) {
                    JOptionPane.showMessageDialog(this, "Code generated sucessfuly");
                }
                else {
                    JOptionPane.showMessageDialog(this, cgcontrol.getReport());
                }                
            } break;
            
            case CONFIGURE_DESIGN: {
                Design project = (Design) userObject;
                DesignConfiguration conf = project.getConfiguration();
                
                configurationPanel panel = new configurationPanel(designRepository);
                panel.setDataWarper(conf, null);
                panel.setLibraryViewerFrame(libraryViewerFrame);
                
                JDialog dialog = new JDialog();
                dialog.setModal(true);
                dialog.setSize(500,300);
                dialog.setLocation(this.getX() + 10, this.getY() + 10);
                
                panel.setOwner(dialog);
                
                dialog.setContentPane(panel);
                dialog.setVisible(true);
                
                if(panel.commited()) {
                    project.getStatus().signalChanged();
                    for(Artifact art : project.artifacts()) {
                        if(art.getType() == ElementType.ATOMICCOMP ||
                                art.getType() == ElementType.COMPOSITECOMP) {
                            RuleCheckingStatus rstat = ((ComponentArtifact)art).getRuleCheckingStatus();
                            rstat.setChecked(false);
                        }   
                    }
                } 
            } break;
            
            ////////////////////////////////////////////////// Sobre um artefato
            
            case VIEW_ARTIFACT: {
                /*
                Artifact art = (Artifact) userObject;
                DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();
                Design d = (Design) parent.getUserObject();

                JPanel panel = null;
                if(art.getType() == ElementType.ATOMICCOMP) {
                    AtomicComponent aa = (AtomicComponent) art;
                    RuleCheckingStatus cstatus = aa.getRuleCheckingStatus();
                    
                    if(!cstatus.wasChecked() ||
                       !cstatus.hasPassed()) {
                        JOptionPane.showMessageDialog(this, "In order to view component, it must " +
                                " have been checked.");
                        return;
                    }
                    else {
                        String s = cstatus.getRuleConfiguration();
                        RuleConfiguration rconf = d.getConfiguration().getRuleConfiguration(s);
                        if(!rconf.includeSyntaxRules()) {
                            JOptionPane.showMessageDialog(this, "In order to view component, it " +
                                " must have been checked.");
                            return;
                        }
                    }

                    Acomp comp = aa.getRootNode();
                    Iterator vite = comp.versionsIterator();
                    LinkedList <String> vers = new LinkedList<String>();
                    while(vite.hasNext()) {
                        vers.add( ((Ver)vite.next()).getName() );
                    }
                    Object[] possibleValues = vers.toArray();
                    if(possibleValues.length == 0) {
                        JOptionPane.showMessageDialog(this, "Component must have at least one" +
                            " version.");
                        return;
                    }
                    
                    Object selectedValue;
                    if(possibleValues.length == 1) {
                        selectedValue = possibleValues[0];
                    }
                    else {
                       selectedValue = JOptionPane.showInputDialog(null,
                      "Choose version", "Versions", JOptionPane.INFORMATION_MESSAGE, 
                            null,possibleValues, possibleValues[0]);
                    }
                    AtomicComponentViewPanel avp = new AtomicComponentViewPanel(d.getDesignLocation(),
                            comp, (Ver)comp.getVersion((String)selectedValue));
                    panel = avp;
                }
                else
                if(art.getType() == ElementType.COMPOSITECOMP) {
                    CompositeComponent ca = (CompositeComponent) art;                    
                    RuleCheckingStatus cstatus = ca.getRuleCheckingStatus();
                    
                    if(!cstatus.wasChecked() ||
                       !cstatus.hasPassed()) {
                        JOptionPane.showMessageDialog(this, "In order to view component, it must " +
                                " have been checked.");
                        return;
                    }
                    else {
                        String s = cstatus.getRuleConfiguration();
                        RuleConfiguration rconf = d.getConfiguration().getRuleConfiguration(s);
                        if(!rconf.includeSyntaxRules()) {
                            JOptionPane.showMessageDialog(this, "In order to view component, \nits" +
                                " syntax must have been checked.");
                            return;
                        }
                    }

                    Topology top = ca.getRootNode();
                    Iterator vite = top.versionsIterator();
                    LinkedList <String> vers = new LinkedList<String>();
                    while(vite.hasNext()) {
                        vers.add( ((Hver)vite.next()).getName() );
                    }
                    Object[] possibleValues = vers.toArray();
                    if(possibleValues.length == 0) {
                        JOptionPane.showMessageDialog(this, "Component must have at least one" +
                                " version.");
                        return;
                    }
                    
                    Object selectedValue;
                    if(possibleValues.length == 1) {
                        selectedValue = possibleValues[0];
                    }
                    else {
                       selectedValue = JOptionPane.showInputDialog(null,
                      "Choose version", "Versions", JOptionPane.INFORMATION_MESSAGE, 
                            null,possibleValues, possibleValues[0]);
                    }

                    TopologyGraph tg = new TopologyGraph();
                    CompositeComponentViewPanel vp = new CompositeComponentViewPanel(
                            tg.generateViewGraph(top,
                                (Hver)top.getVersion((String)selectedValue)));
                    panel = vp;
                }
                else {
                    return;
                }

                JFrame jf = new JFrame();
                jf.getContentPane().add(panel);
                jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                jf.setSize(500,600);
                jf.setVisible(true);
                 */
            } break;
            
            case REMOVE_ARTIFACT: {
                Artifact art = (Artifact) userObject;
                
                // 1. Pergunta se tem certeza
                int answer = JOptionPane.showConfirmDialog(this, "Are you sure about " +
                        "removing artifact:\n" + art.toString());
                if(answer == JOptionPane.NO_OPTION ||
                    answer == JOptionPane.CANCEL_OPTION) {
                    return;
                } 
                // 2. Fechar todas as visualizacoes deste projeto
                
                // eu considero que um design so possui um nivel de artefatos. Entao
                // o pai de um artefato eh um design.
                DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();
                Design d = (Design) parent.getUserObject();
                                
                d.closeArtifactEditor(art);
                
                // 3. Destroi o arquivo ou nao ?
                answer = JOptionPane.showConfirmDialog(this, "Remove artifact file ?");
                if(answer == JOptionPane.YES_OPTION) {
                    URL url = art.getURL();
                    File fd = new File(art.getBaseDir() + 
                            url.getFile().toString());
                    fd.delete();
                }

                // 4. Remover do design, o artefato em questao
                d.removeArtifact(art);

                projectTreeState.reloadTree();
                editorPane.repaint();
            } break;

            case EDIT_ARTIFACT: {
                Artifact art = (Artifact) userObject;
                DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();
                Design d = (Design) parent.getUserObject();
                
                // 1. registra que o artefato esta sendo editado
                if(d.isEditingArtifact(art)) {
                    ArtifactEditor editor = (ArtifactEditor) d.getArtifactEditor(art);
                    editor.refreshDisplay();
                    return;
                }

                // 2. cria o editor, registra ele, e mostra na tela
                ArtifactEditor editor = null;                
                switch(art.getEditorType()) {
                    case ATOMIC: {
                        editor = new AtomicComponentEditor(d, (AtomicComponent)art, editorPane, 
                                configuration);
                        if(!editor.initializedOk()) {
                            JOptionPane.showMessageDialog(this, "Could not edit artifact:\n" +
                                    art.getName());
                            return;
                        }
                        //((ComponentArtifactEditor) editor).setServicesFrame(servicesFrame);
                        ((ComponentArtifactEditor) editor).setLibraryViewer(libraryViewerFrame);
                    } break;

                    case COMPOSITE: {
                        editor = new CompositeComponentEditor(d, (CompositeComponent) art,
                                editorPane, configuration, designRepository);
                        if(!editor.initializedOk()) {
                            JOptionPane.showMessageDialog(this, "Could not edit artifact:\n" +
                                    art.getName());
                            return;
                        }
                        //((ComponentArtifactEditor) editor).setServicesFrame(servicesFrame);
                        ((ComponentArtifactEditor) editor).setLibraryViewer(libraryViewerFrame);
                    } break;
                    
                    case RULE: {
                        editor = new RuleEditor(d, (RuleElement) art, editorPane, 
                                configuration, designRepository);
                        if(!editor.initializedOk()) {
                            JOptionPane.showMessageDialog(this, "Could not edit artifact:\n" +
                                    art.getName());
                            return;
                        }
                    }break;
                }
                d.addArtifactEditor(art, editor);
                editor.refreshDisplay();
                
               // projectTreeState.repaintTree();
            } break;
            
            case SAVE_ARTIFACT: {
                Artifact art = (Artifact) userObject;
                DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();
                Design d = (Design) parent.getUserObject();
                if(art.getStatus().changedStatus()) {
                    try {
                        saveArtifact(d, art);
                    }
                    catch(IOException ex) {
                        System.out.println(ex);
                    }
                }
            } break;
            
            case CHECK_ARTIFACT: {
                Artifact art = (Artifact) userObject;
                if(art.getEditorType() != EditorType.ATOMIC &&
                        art.getEditorType() != EditorType.COMPOSITE) {
                    JOptionPane.showMessageDialog(this, "Cannot check rules of " +
                            "a non component artifact");
                    return;
                }
                DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();
                Design d = (Design) parent.getUserObject();
  
                JDialog dialog = new JDialog();
                dialog.setModal(true);
                dialog.setSize(500,200);
                dialog.setLocation(this.getX() + 10, this.getY()+ 10);

                PickRuleConfigurationPanel pc = new PickRuleConfigurationPanel(dialog,
                        d.getConfiguration());
                dialog.setContentPane(pc);
                dialog.setVisible(true);
                if(pc.isOk()) {
                    ComponentArtifact aed = (ComponentArtifact) art;
                    RuleConfiguration rconf = pc.getSelectedRuleConfiguration();

                    RuleCheckingStatus stat = aed.getRuleCheckingStatus();
                    if(stat.wasChecked() && stat.hasPassed() && 
                            stat.getRuleConfiguration().equals(rconf.getName())) return;
   
                    checkRules(rconf, aed);
                    d.getStatus().signalChanged();
                }
            } break;
            
            case VIEW_CHECK_RESULTS : {
                Artifact art = (Artifact) userObject;
                if(art.getEditorType() != EditorType.ATOMIC &&
                        art.getEditorType() != EditorType.COMPOSITE) {
                    return;
                }                
                ComponentArtifact compart = (ComponentArtifact) art;
                RuleCheckingStatus rstats = compart.getRuleCheckingStatus();
                
                if(!rstats.wasChecked() || rstats.hasPassed()) return;
                
                CheckingRulesResultFrame rframe = new CheckingRulesResultFrame();
                rframe.setSize(500,400);
                rframe.setLocation(this.getX() + 10, this.getY()+ 10);
                
                rframe.ruleConfigLabel.setText(rstats.getRuleConfiguration());                
                DefaultListModel model = (DefaultListModel) rframe.ruleNameList.getModel();
                for(String s : rstats.getRules()) {
                    model.addElement(s);
                }
                rframe.resultTextArea.setText(rstats.getReport());
                rframe.setVisible(true);
            } break;
        }
    }

    //////////////////////////////////////// PRIVATE METHODS ///////////////////
    
    private void clearNewProjectDialog() {
        newProjectDialog_jTextField1.setText("");
        newProjectDialog_jTextField2.setText("");
    }
 
    private void clearAddArtifactDialog() {
        addArtifactDialog_nameTextField.setText("");
        addArtifactOk = false;
    }
    
    private void deleteDir(File fd) {
        File [] files = fd.listFiles();
        for(int i = 0;i < files.length;i++) {
            files[i].delete();    
        }
        fd.delete();
    }
    
    // This is the only method that can reset the stauts of a meta-artifact
    private final void saveArtifact(Design d, Artifact artifact) throws IOException {
        StringBuffer buf = new StringBuffer();
        if(artifact.getEditorType() == EditorType.ATOMIC) {
            AtomicComponent aa = (AtomicComponent) artifact;
            Acomp comp = aa.getRootNode();
            if(comp == null) {
                System.out.println(AtomicComponentParser.errorMessage);
                return;
            }
            buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "<!DOCTYPE atomicComponent PUBLIC \"-//ODOGSYNTAXATOMIC//DTD//EN\" "+
                    "\"\">\n");
            buf.append(comp.exportXML(0));
        }
        else
        if(artifact.getEditorType() == EditorType.COMPOSITE) {
            CompositeComponent ca = (CompositeComponent) artifact;
            Topology top = ca.getRootNode();
            if(top == null) {
                System.out.println(TopologyParser.errorMessage);
                return;
            }
            buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "<!DOCTYPE topology PUBLIC \"-//ODOGSYNTAXTOPOLOGY//DTD//EN\" "+
                    "\"\">\n");
            buf.append(top.exportXML(0));
        }
        else {
            RuleElement re = (RuleElement) artifact;
            Rule r = re.getRule();
            buf.append(r.getExpression());
        }         

        File f = new File(d.getDesignLocation() + artifact.getURL().getFile());    
        FileOutputStream fos = new FileOutputStream(f);
        PrintStream ps = new PrintStream(fos);

        ps.print(buf.toString());

        ps.close();
        fos.close();

        artifact.getStatus().resetChanged();
    }

    private void checkRules(RuleConfiguration conf, ComponentArtifact art) {
        boolean extraSyntaxChecks = false;
        boolean extraCGChecks = false;
        if(conf == null) {
            JOptionPane.showMessageDialog(this, "No rule configuration selected.");
            return;
        }
        Checker c = new Checker();

        RuleCheckingStatus status = art.getRuleCheckingStatus();        
        Iterator ite = conf.ruleDescriptionsIterator();
        addRulesToChecker(c, ite, status);

        if(conf.includeSyntaxRules()) {
            ite = conf.syntaxRulesIterator();
            addRulesToChecker(c, ite, status);
            extraSyntaxChecks = true;
        }
        
        if(conf.includeCGRules()) {
            ite = conf.cgRulesIterator();
            addRulesToChecker(c, ite, status);
            extraCGChecks = true;
        }
        
        if(art instanceof AtomicComponent) {
            c.checkAtomicComponent(((AtomicComponent)art).getRootNode());
        }
        else {            
            c.checkTopology(((CompositeComponent)art).getRootNode());
        }
        
        status.setChecked(true);
        status.setVersionChecked(((Artifact) art).getStatus().getVersion());
        status.setRuleConfiguration(conf.getName());
        if(c.checkPass()) {            
            if(art instanceof CompositeComponent) {
                boolean res = true;
                if(extraSyntaxChecks) {
                    res = c.extraSyntaxChecks(((CompositeComponent)art).getRootNode());
                }
                if(!res) {
                    status.setPassed(false);
                    status.setReport(c.getExtraCheckErrorMessage());
                    JOptionPane.showMessageDialog(this, "CHECK FAIL!");
                    return;
                }
                if(extraCGChecks) {
                    res = c.extraCGChecks(((CompositeComponent)art).getRootNode());
                }
                if(!res) {
                    status.setPassed(false);
                    status.setReport(c.getExtraCheckErrorMessage());
                    JOptionPane.showMessageDialog(this, "CHECK FAIL!");
                    return;
                }                
            }

            JOptionPane.showMessageDialog(this, "CHECK PASS!");
            status.setPassed(true);
        }
        else {   
            status.setPassed(false);

            String res = "";
            ite = conf.ruleDescriptionsIterator();
            res = addRulesErrorMessages(res, ite);
            
            if(conf.includeSyntaxRules()) {
                ite = conf.syntaxRulesIterator();
                res = addRulesErrorMessages(res, ite);
            }
            
            if(conf.includeCGRules()) {
                ite = conf.cgRulesIterator();
                res = addRulesErrorMessages(res, ite);
            }
            
            status.setReport(res);
            JOptionPane.showMessageDialog(this, "CHECK FAIL!");
        }               
    }

    private void addRulesToChecker(Checker c, Iterator ite, RuleCheckingStatus status) {
        while(ite.hasNext()) {
            // loads the next rule to be checked
            RuleDescription rd = (RuleDescription) ite.next();
            Design lib = designRepository.findAndParseLib(rd.getLibraryURL());
            if(lib == null) {
                JOptionPane.showMessageDialog(this, "Could not find lib:\n" + 
                        rd.getLibraryURL() + "\nYour rule configuration must be outdated.");
                return;
            }
            RuleElement ruleElement = (RuleElement) lib.getArtifact(rd.getName());
            if(ruleElement == null) {
                JOptionPane.showMessageDialog(this, "Could not find rule:\n" + 
                        rd.getName() + "\nYour rule configuration must be outdated.");
                return;
            }
            c.addRule(ruleElement.getRule());
            status.addRule(rd.getName());
        }        
    }
    
    private String addRulesErrorMessages(String res, Iterator ite) {
        while(ite.hasNext()) {
            RuleDescription rd = (RuleDescription) ite.next();
            Design lib = designRepository.findAndParseLib(rd.getLibraryURL());
            RuleElement ruleElement = (RuleElement) lib.getArtifact(rd.getName());
            Rule r = ruleElement.getRule();
            if(!r.getResult()) {
                res = res + "Rule " + ruleElement.getName() + " failed.\n" +
                        ruleElement.getSummary() + "\n";
                for(String em : r.errorMessages()) {
                    res = res + em;
                }
                res = res + "\n";
            }                
        }
        return res;
    }

    private LinkedList<Design> loadEnvironmentLibs() {
        LinkedList<Design> ret = new LinkedList<Design>();
        for(String s : configuration.getOdogLibraries()) {
            File f = new File(s);
            if(!f.isDirectory()) continue; 
            String [] files = f.list();
            for(int j = 0;j < files.length;j++) {
                Design d = designRepository.loadDesignFromFile(s + files[j], true);
                if(d != null) {
                    ret.add(d);
                }
            }
        }
        return ret;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem addArtifact;
    private javax.swing.JDialog addArtifactDialog;
    private javax.swing.JList addArtifactDialog_artifactList;
    private javax.swing.JTextField addArtifactDialog_nameTextField;
    private javax.swing.JMenuItem checkArtifact;
    private javax.swing.JFrame checkResultsFrame;
    private javax.swing.JTextArea checkResultsTextArea;
    private javax.swing.JMenuItem checkRules;
    private javax.swing.JMenuItem configure;
    private javax.swing.JMenuItem editArtifact;
    private javax.swing.JLayeredPane editorPane;
    private javax.swing.JMenuItem generateCode;
    private javax.swing.JMenuItem importArtifact;
    private javax.swing.JMenuItem importProjectMenuItem;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JSeparator jSeparator6;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JFileChooser locationChooser;
    private javax.swing.JMenuBar mainMenu;
    private javax.swing.ButtonGroup newProjectButtonGroup;
    private javax.swing.JDialog newProjectDialog;
    private javax.swing.JRadioButton newProjectDialog_LibraryButton;
    private javax.swing.JRadioButton newProjectDialog_SystemButton;
    private javax.swing.JTextField newProjectDialog_jTextField1;
    private javax.swing.JTextField newProjectDialog_jTextField2;
    private javax.swing.JMenuItem newProjectMenuItem;
    private javax.swing.JPanel pane3;
    private javax.swing.JPopupMenu projectArtifactMenu;
    private javax.swing.JPopupMenu projectMenu;
    private javax.swing.JTree projectTree;
    private javax.swing.JPopupMenu projectTreeMenu;
    private javax.swing.JMenuItem removeArtifact;
    private javax.swing.JMenuItem removeProject;
    private javax.swing.JMenuItem saveArtifact;
    private javax.swing.JMenuItem viewArtifact;
    private javax.swing.JMenuItem viewCheckResults;
    // End of variables declaration//GEN-END:variables
 
    private AboutFrame aboutFrame;
    
    private TreeState projectTreeState;
    private BaseConfiguration configuration;
    private LibraryViewerFrame libraryViewerFrame;
    // private ServicesFrame servicesFrame;
 
    private DesignRepository designRepository;
    
    private LinkedList<Design> environmentLibs;  // libraries from ODOG_LIBRARIES
    
    private boolean addArtifactOk;
    private boolean configureDesignOk;

    // As possiveis acoes tratadas
    private enum ActionType { NEW_DESIGN, 
        REMOVE_DESIGN, IMPORT_DESIGN, ADD_ARTIFACT,
        VIEW_COMPONENT, GENERATE_CODE, CHECK_RULES,
        CONFIGURE_DESIGN, VIEW_ARTIFACT, REMOVE_ARTIFACT,
        EDIT_ARTIFACT, IMPORT_ARTIFACT, SAVE_ARTIFACT,
        CHECK_ARTIFACT, VIEW_CHECK_RESULTS, SAVE_DESIGN  }

    private enum MsgType { ERROR_MSG, WARNING_MSG, INFO_MSG }  
}